﻿using HomagChina.CCT.DataTypeModel;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Themes.Resources;
using HomagChina.CCT.Router;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using System.Windows.Media;
using static HomagChina.CCT.ProcessResourceInternal;
using static HomagChina.CCT.Utility;
using Icon = HomagGroup.Base.UI.Themes.Resources.Icon;

namespace HomagChina.CCT.ViewModels
{
    public class ErrorMessageViewModel : NotifyPropertyChangedBase
    {
        private SearchInfoHandle Handle_Search;
        private ObservableCollection<ErrorMessageModel> _List_MessageModel;

        public ObservableCollection<ErrorMessageModel> List_MessageModel
        {
            get => _List_MessageModel;
            set { _List_MessageModel = value; OnPropertyChanged(); }
        }

        private bool _FilterAll;

        public bool FilterAll
        {
            get => _FilterAll;
            set { _FilterAll = value; OnPropertyChanged(); }
        }

        private bool _Error;

        public bool Error
        {
            get => _Error;
            set { _Error = value; OnPropertyChanged(); }
        }

        private bool _Warning;

        public bool Warning
        {
            get => _Warning;
            set { _Warning = value; OnPropertyChanged(); }
        }

        private double _Height = 75;

        public double Height
        {
            get => _Height;
            set { _Height = value; OnPropertyChanged(); }
        }

        private System.Windows.Media.Brush _TableBack;

        public System.Windows.Media.Brush TableBack
        {
            get => _TableBack;
            set { _TableBack = value; OnPropertyChanged(); }
        }

        public RelayCommand HandleChangeFilterCommand { get; }
        public RelayCommand HandleRefreshCommand { get; }
        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }
        public IDictionary<int, Brush> CustomStatus { get; set; }
        private static DispatcherTimer timer;

        private string messageType;

        public ErrorMessageViewModel()
        {
            Handle_Search = new SearchInfoHandle();
            HandleChangeFilterCommand = new RelayCommand(HandleChangeFilter);
            HandleRefreshCommand = new RelayCommand(HandleRefresh);
            LoadedCommand = new RelayCommand(ViewloadedCommand);
            UnLoadedCommand = new RelayCommand(ViewUnloadedCommand);
            timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(1500)
            };
            CustomStatus = new Dictionary<int, Brush>
            {
                 { 0, Application.Current.TryFindResource(InternalKeys.ErrorImage) as Brush },
                { 1, Application.Current.TryFindResource(InternalKeys.MessageExclamationImage) as Brush }
            };
        }

        /// <summary>
        ///
        /// </summaryzal
        public void ViewloadedCommand()
        {
            FilterAll = Warning = Error = true;
            messageType = "All";
            Refresh();
        }

        /// <summary>
        /// Unloaded
        ///
        /// </summary>
        private void ViewUnloadedCommand()
        {
            timer.Stop();
        }

        public void Refresh()
        {
            timer.Tick += (sender, args) =>
            {
                //if (NavigateServices.Instance.CurrentRouteInfo.functionKeyBarItems[2].MenuItems[0].IsChecked)
                List_MessageModel = Handle_Search.GetErrorMessageMation(messageType);
            };

            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                timer.Start();
            }
        }

        /// <summary>
        ///
        /// </summary>
        private void HandleChangeFilter()
        {
            FilterAll = (Error && Warning);
            if (FilterAll == false)
            {
                messageType = Error ? "Error" : Warning ? "Warning" : "Empty";
            }
            else
            {
                messageType = "All";
            }
        }

        private void HandleRefresh()
        {
            Error = Warning = FilterAll;
            if (FilterAll == false)
            {
                messageType = "Empty";
            }
            else
            {
                messageType = "All";
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="errorMessageRouterItem"></param>
        public void HandleIsShowFilter(object errorMessageRouterItem)
        {
            var MainWindow = Application.Current.MainWindow;
            var Filter = (ContentControl)MainWindow.FindName("contentControl");
            var content = Filter.Content as FrameworkElement;
            var element = content.FindName("FilterElement") as FrameworkElement;
            var startBoard = new Storyboard();
            var animation = new DoubleAnimation();
            var currentStatus = NavigateServices.Instance.CurrentRouteInfo.functionKeyBarItems[0];
            if (currentStatus.Header as string == GetLocalText("Filter_off"))
            {
                animation = new DoubleAnimation()
                {
                    From = 0,
                    To = 75,
                    Duration = TimeSpan.FromSeconds(0.5)
                };

                currentStatus.Header = GetLocalText("Filter_on");
                currentStatus.Icon = GetIconBrush(Icon.ViewGrid, overlayIcon: OverlayIcon.Check);
            }
            else
            {
                animation = new DoubleAnimation()
                {
                    From = 75,
                    To = 0,
                    Duration = TimeSpan.FromSeconds(0.5)
                };

                currentStatus.Header = GetLocalText("Filter_off");
                currentStatus.Icon = GetIconBrush(Icon.ViewGrid, overlayIcon: OverlayIcon.Check, crossedOut: true);
            };
            startBoard.Children.Add(animation);
            Storyboard.SetTargetProperty(animation, new PropertyPath("Height"));
            Storyboard.SetTarget(animation, element);
            startBoard.Begin();
        }

        public void HandleIsFreeze()
        {
            switch (NavigateServices.Instance.CurrentRouteInfo.functionKeyBarItems[1].MenuItems[0].IsChecked)
            {
                case true:
                    timer.Stop();
                    TableBack = GetHexadecimalColor("#ffdd9c");
                    NavigateServices.Instance.CurrentRouteInfo.functionKeyBarItems[1].MenuItems[1].IsEnabled = false;
                    break;

                case false:
                    timer.Start();
                    TableBack = null;
                    NavigateServices.Instance.CurrentRouteInfo.functionKeyBarItems[1].MenuItems[1].IsEnabled = true;
                    break;

                default:
                    break;
            }
        }

        public void HandleIsHistory()
        {
            switch (NavigateServices.Instance.CurrentRouteInfo.functionKeyBarItems[1].MenuItems[1].IsChecked)
            {
                case true:
                    TableBack = GetHexadecimalColor("#f79898");
                    NavigateServices.Instance.CurrentRouteInfo.functionKeyBarItems[1].MenuItems[0].IsEnabled = false;
                    break;

                case false:
                    TableBack = null;
                    NavigateServices.Instance.CurrentRouteInfo.functionKeyBarItems[1].MenuItems[0].IsEnabled = true;
                    break;

                default:
                    break;
            }
        }
    }
}